﻿using System;
using System.Linq;
using db = DataContext;

namespace WebApplication.admin
{
	public partial class Company : System.Web.UI.Page
	{
		public string tag;
		public int id;

		protected void Page_Load(object sender, EventArgs e)
		{
			Common.CheckLogin();

			tag = Common.Query("tag");
			id = Common.Id;

			using (db.DbDataContext context = new db.DbDataContext())
			{
				if (Common.IsSave)
				{
					db.Config config = context.Config.Single(x => x.Id == id);
					config.Value = Request.Form["content"];
					context.SubmitChanges();

					Response.Redirect("company.aspx?tag=" + config.Name, true);
				} else
				{
					IQueryable<db.Config> configs = context.Config.Where(x => x.Name == tag);
					if (configs.Count() == 0)
					{
						db.Config config = new db.Config { Name = tag, Value = "" };
						context.Config.InsertOnSubmit(config);
						context.SubmitChanges();
						id = config.Id;
					} else
					{
						db.Config config = configs.Single();
						content.Value = config.Value;
						id = config.Id;
					}
				}
			}
		}
	}
}